package com.atguigu.gmall.cart.service.impl;

import com.atguigu.gmall.cart.mapper.CartInfoMapper;
import com.atguigu.gmall.cart.service.CartInfoAsyncService;
import com.atguigu.gmall.product.client.model.cart.CartInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
public class CartInfoAsyncServiceImpl implements CartInfoAsyncService {

    @Autowired
    private CartInfoMapper cartInfoMapper;

    //2. 写mysql 异步  打注解的方式 更新数据到数据库  对redis的备份   异步保存  异步调用
    @Override
    @Async
    public void updateByIdAsync(CartInfo cartInfo) {

        //cartInfoMapper.updateById(cartInfo);
        System.out.println("Async :" +Thread.currentThread().getName());  //打印异步调用线程名
         cartInfoMapper.update(cartInfo,new QueryWrapper<CartInfo>()
                .eq("user_id",cartInfo.getUserId())
                .eq("sku_id",cartInfo.getSkuId()));

    }

    //2. 写mysql 异步  保存数据到数据库  对redis的备份     异步保存  异步调用
    @Override
    @Async
    public void insertAsync(CartInfo cartInfo) {
        System.out.println("Async :" +Thread.currentThread().getName());  //打印异步调用线程名
        cartInfoMapper.insert(cartInfo);
    }


    @Override
    @Async
    public void updateByIdAsyncExt(String userTempId, String userId) {

        System.out.println("Async : updateByIdAsyncExt :" +Thread.currentThread().getName());  //打印异步调用线程名

        CartInfo cartInfo = new CartInfo();
        cartInfo.setUserId(userId);
        cartInfoMapper.update(cartInfo,new QueryWrapper<CartInfo>()
                                .eq("user_id",userTempId));
    }

    @Override
    @Async        //删除
    public void deleteByUserTempId(String userTempId) {

        System.out.println("Async : deleteByUserTempId :" +Thread.currentThread().getName());  //打印异步调用线程名
        cartInfoMapper.delete(new QueryWrapper<CartInfo>().eq("user_id",userTempId));
    }
}
